package com.yutel.test.ip;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.com.xinli.mobile.packet.AbstractPacket;
import cn.com.xinli.mobile.packet.Protocol;

public class NetUtils {
	public List<String> list = new ArrayList<String>();

	public String ip() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						System.out.println("IP:"
								+ inetAddress.getHostAddress().toString());
						return inetAddress.getHostAddress().toString();
					}
				}
			}
			return null;
		} catch (SocketException ex) {
			System.out.println(ex.toString());
			return null;
		}
	}

	public void server() {
		ExecutorService pool = Executors.newFixedThreadPool(20);
		String ip = pcip();
		int index = ip.lastIndexOf('.');
		String subip = ip.substring(0, index + 1);
		for (int i = 1; i < 255; i++) {
			String pingid = subip + i;
			scan t = new NetUtils.scan(pingid);
			pool.submit(t);
		}
		finish p = new NetUtils.finish();
		pool.submit(p);
		pool.shutdown();
	}

	public String pcip() {
		try {
			InetAddress inet = InetAddress.getLocalHost();
			System.out.println("ip=" + inet.getHostAddress());
			return inet.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		NetUtils nu = new NetUtils();
		// pcip();
		nu.server();
	}

	public class scan extends Thread {
		private String ip;

		public scan(String ip) {
			this.ip = ip;
		}

		@Override
		public void run() {
			Socket client = null;
			ObjectInputStream in = null;
			ObjectOutputStream out = null;
			try {
				AbstractPacket ap = new AbstractPacket(Protocol.Command.ack);
				ap.setValue("ack");
				// System.out.println("connect");
				client = new Socket();
				InetAddress addr = Inet4Address.getByName(ip);
				client.connect(new InetSocketAddress(addr, 9080), 1000);

				// System.out.println("write");
				out = new ObjectOutputStream(client.getOutputStream());
				out.writeObject(ap);
				out.flush();

				// System.out.println("read");
				in = new ObjectInputStream(client.getInputStream());
				Object ok = in.readObject();
				System.out.println(ok.toString());
				list.add(ip);
				System.out.println("close=" + ip + ",size=" + list.size());
				in.close();
				out.close();
				client.close();
			} catch (Exception e) {
				//e.printStackTrace();
			} finally {
				try {
					if (in != null)
						in.close();
					if (out != null)
						out.close();
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public class finish extends Thread {
		@Override
		public void run() {
			System.out.println("list finish,size=" + list.size());
		}
	}
}
